from Test import AbstractTest
from MySocket import User, Event
'''
1. Create 3 users
2. Users #1, #3 follows user #2, user #2 receives 2 notifications
3. User #2 sends update message, that is received by #1 and #3
4. User #3 unfollows user #2
5. User #2 sends update message, that is received by #1 but not #3
'''
class TestUnfollowMessage(AbstractTest):
    def __init__(self):
        test_name = "Unfollow Message Test"
        super(TestUnfollowMessage, self).__init__(test_name)

    def run(self):
        # message to be sent
        message1 = "1|F|1|2\n"
        message2 = "2|F|3|2\n"
        user_1 = User()
        user_2 = User()
        user_3 = User()
        # connect all the users
        user_1.connect()
        user_2.connect()
        user_3.connect()
        # send id
        user_1.mysend("1\n")
        user_2.mysend("2\n")
        user_3.mysend("3\n")
        # create event
        event = Event()
        event.connect()

        # user 1 follow user 2
        event.mysend(message1)
        if(user_2.myreceive(len(message1)) != message1):
            return False

        # user 3 follow user 2
        event.mysend(message2)
        if(user_2.myreceive(len(message2)) != message2):
            return False

        # now sent status update message and check if user 1 and 3 gets it
        message3 = "3|S|2\n"
        event.mysend(message3)
        if(user_1.myreceive(len(message3)) != message3):
        	return False
       	if(user_3.myreceive(len(message3)) != message3):
        	return False

       	# now sent a message to remove follower
       	message4 = "4|U|3|2\n"
       	event.mysend(message4)

       	# and sent update status to check if user 3 won't receive it
       	message5 = "5|S|2\n"
       	event.mysend(message5)
       	if(user_1.myreceive(len(message5)) != message5):
        	return False
       	
       	return True
